'use strict';
/**
 * 角色权限管理Service
 *
 * @see DeviceController.java
 *
 */
app.service('DeviceService',
    ['$http', '$q', 'dialogs',
        function ($http, $q, dialogs) {
            var that = this;
            this.getGridOptions = function () {
                var _gridWidth = 1024;
                var _columnWidth = _gridWidth / 7;
                return {
                    //height: getGridHeight(),
                    url: ISS.getPath('/EssDevice/list'),
                    dataType: 'json',
                    // contentType: 'json',
                    colModel: [
                        {display: 'device.snCode', name: 'deviceCoding', width: _columnWidth, sort: 'true'},
                        {display: 'device.name', name: 'deviceName', width: _columnWidth},
                        {
                            display: 'device.deviceType.deviceType',
                            name: 'deviceType',
                            width: _columnWidth,
                            process: function (value, index) {
                                return value == 0 ? "PM10" : "监控设备";
                            }
                        },
                        {display: 'device.installTime', name: 'installTime', width: _columnWidth},
                        {
                            display: 'device.deviceLocation', name: 'arch.name', width: _columnWidth
                        },
                        {
                            display: 'vs.monitorRecord.deviceStatus',
                            name: 'deviceStatus',
                            width: _columnWidth * 0.6,
                            process: function (value, index) {
                                return value == 0 ? "在线" : value == 1 ? "离线" : "故障";
                            }
                        },
                        {
                            display: 'common.operation',
                            name: "deviceId",
                            width: 150,
                            process: function (value, index) {
                                var html = [];
                                html.push('<button iss-permission="B_DEVICE_UPDATE" class="btn m-r-xs btn-xs btn-primary" id="updateBtn" ng-click="vm.updateDevice(' + value + ')">');
                                html.push('<i class="fa fa-pencil"></i>');
                                html.push('<span translate="common.edit"></span>');
                                html.push('</button>');
                                html.push('<button iss-permission="B_DEVICE_DELETE" class="btn m-r-xs btn-xs btn-danger" id="delBtn" ng-click="vm.delDevice(' + index + ')">');
                                html.push('<i class="fa fa-trash-o"></i>');
                                html.push('<span translate="common.del"></span>');
                                html.push('</button>');
                                return html.join('');
                            }
                        }
                    ],
                    usepager: true,
                    pageMode: 'jpa',
                    showcheckbox: true,
                    rp: 10,
                    sortorder: 'asc',
                    isDragCol: true
                };
            }
            this.getDevice = function (deviceId) {
                return $http.post(
                    ISS.getPath('/EssDevice/getDevice/' + deviceId)
                ).then(function (resp) {
                    return resp.data;
                });
            }
            this.addDevice = function (data) {
                return $http.post(
                    ISS.getPath('/EssDevice/add'),
                    data,
                    {contentType: 'json'}
                ).then(function (resp) {
                    return resp.data;
                });
            }
            this.updateDevice = function (data) {
                return $http.post(
                    ISS.getPath('/EssDevice/update'),
                    data,
                    {contentType: 'json'}
                ).then(function (resp) {
                    return resp.data;
                });
            }


            this.delDevice = function (data) {
                return $http.post(
                    ISS.getPath('/EssDevice/delDevice/' + data.deviceId + '/' + data.deviceName),
                    {contentType: 'json'}
                ).then(function (resp) {
                    return resp.data;
                });
            }

            // var archList = [];
            this.getDeviceLocation = function () {
                return $http.post(
                    ISS.getPath('/area/getArchList')
                ).then(function (resp) {
                    // archList = resp.data;
                    return resp.data;
                });
            }

            this.setThreshold = function (data) {
                return $http.post(
                    ISS.getPath('/EssDevice/setThreshold'),
                    data,
                    {contentType: 'json'}
                ).then(function (resp) {
                    return resp.data;
                });
            }

            // var threshold = [];
            this.getThreshold = function (firstData) {
                return $http.post(
                    ISS.getPath('/EssDevice/getThreshold/' + firstData)
                ).then(function (resp) {
                    // threshold = resp.data;
                    return resp.data;
                });
            }

        }]);